package com.zmx.core.executor;

import com.zmx.core.client.invokemethod.InvokeMethod;
import com.zmx.core.handler.IJobHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @Description: 任务执行基类
 * @ClassName: JobExecutor
 * @Author zhaomxr
 * @Date 2021-12-17 11:08
 */
@Slf4j
public class JobExecutor {

    private int port;
    private String username;
    private String password;
    private String secret;
    private int maxMeanwhile;

    public void setPort(int port) {
        this.port = port;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public void setMaxMeanwhile(int maxMeanwhile) {
        this.maxMeanwhile = maxMeanwhile;
    }

    // ---------------------- start + stop ----------------------
    public void start() throws Exception {

        // init executor-server
        initInvokeMethod(port, username, password, secret, maxMeanwhile);
    }

    /**
     * job handler repository
     * ------------------------------------------------------------------------------------------
     */
    private static ConcurrentMap<String, IJobHandler> jobHandlerRepository = new ConcurrentHashMap<String, IJobHandler>();

    public static IJobHandler loadJobHandler(String name) {
        return jobHandlerRepository.get(name);
    }

    public static IJobHandler registJobHandler(String name, IJobHandler jobHandler) {
        log.info(">>>>>>>>>>> mall-job register jobhandler success, name:{}, jobHandler:{}", name, jobHandler);
        return jobHandlerRepository.put(name, jobHandler);
    }
    //------------------------------------------------------------------------------------------

    /**
     * running job handler repository
     * ------------------------------------------------------------------------------------------
     */
    private static ConcurrentMap<String, IJobHandler> jobRunningHandlerRepository = new ConcurrentHashMap<String, IJobHandler>();

    public static IJobHandler loadRunningJobHandler(String name) {
        return jobRunningHandlerRepository.get(name);
    }

    public static IJobHandler registRunningJobHandler(String name, IJobHandler jobHandler) {
        return jobRunningHandlerRepository.put(name, jobHandler);
    }

    public static void destoryRunningJobHandler(String name) {
        jobRunningHandlerRepository.remove(name);
    }

    public static boolean isRunning(String name) {
        if (loadRunningJobHandler(name) != null) {
            return true;
        }
        return false;
    }
    //------------------------------------------------------------------------------------------

    private InvokeMethod invokeMethod;

    private void initInvokeMethod(int port, final String username, final String password, final String secret, int maxMeanwhile) throws Exception {

        // start
        invokeMethod = new InvokeMethod();
        invokeMethod.start(maxMeanwhile, port, username, password, secret);
    }
}
